/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : system.hpp
  * Description        : 
  ******************************************************************************
  * Function           : 创建任务、信号量、消息队列、软件定时器，详细请看system.cpp文件中的mySystemInit函数
  *
  *
  ******************************************************************************
  * Author             : Zhiping Fu
  * Creation Date      : 2020年10月19日
  ******************************************************************************
  */
/* USER CODE END Header */

#ifndef __SYSTEM_H
#define __SYSTEM_H

#include "FreeRTOS.h"
#include "task.h"
#include "cmsis_os.h"
#include "semphr.h"                     // ARM.FreeRTOS::RTOS:Core
#include "timers.h"
#include "queue.h"

#include "queueMessage.h"

#include "userlib/userlib.hpp"


#ifdef __cplusplus
extern "C"{
#endif
//------------------------------------------------------------------------------------------------------//
//--------------------飞机标号--------------------//
#define plane_num 0
//------------------------------------------------//
//------------------------------------------------------------------------------------------------------//
//自定义全局变量
extern waypoint_msg waypoints;
extern trajectory_msg trajectory_message;


//自己创建的任务
/* Definitions for icm20602Task */
extern TaskHandle_t icm20602TaskHandle;
extern void Icm20602_main(void *argument);
extern SemaphoreHandle_t semIcm20602;

/* Definitions for magnetometerTask */
extern TaskHandle_t lsm303dTaskHandle;
extern void lsm303d_main(void *argument);
extern SemaphoreHandle_t semLsm303d;

/* Definitions for ahrsTask */
extern TaskHandle_t ahrsTaskHandle;
extern void ahrs_main(void *argument);
extern SemaphoreHandle_t semAhrs;

/* Definitions for ekfUwbTask */
extern TaskHandle_t ekfUwb1msTaskHandle;
extern TaskHandle_t ekfUwb5msTaskHandle;
extern void ekfUwb1ms_main(void *argument);
extern void ekfUwb5ms_main(void *argument);
extern SemaphoreHandle_t semEkfUwb1ms;
extern SemaphoreHandle_t semEkfUwb5ms;
extern SemaphoreHandle_t semEkfIns;

/* Definitions for KalUwbTask */
extern TaskHandle_t kalUwbTaskHandle;
extern TaskHandle_t kalUwbPredTaskHandle;
extern void kalUwb_main(void *argument);
extern void kalUwb_pred(void *argument);
extern SemaphoreHandle_t semKalUwb;
extern SemaphoreHandle_t semKalUwbPred;

/* Definitions for tranTask */
extern TaskHandle_t tranTaskHandle;
extern void tran_main(void *argument);
extern SemaphoreHandle_t semTran;

/* Definitions for batteryTask */
extern TaskHandle_t batteryTaskHandle;
extern void battery_main(void *argument);
extern SemaphoreHandle_t semBattery;

/* Definitions for rcTask */
extern TaskHandle_t rcTaskHandle;
extern void rc_main(void *argument);

/* Definitions for motorTask */
extern TaskHandle_t motorTaskHandle;
extern void motor_main(void *argument);
extern SemaphoreHandle_t semMotor;

/* Definitions for attitudeCtrlTask */
extern TaskHandle_t attitudeCtrlTaskHandle;
extern void attitudeCtrl_main(void *argument);
extern SemaphoreHandle_t semAttitudeCtrl;

/* Definitions for geometricCtrlTask */
extern TaskHandle_t geometricCtrlTaskHandle;
extern void geometricCtrl_main(void *argument);
extern SemaphoreHandle_t semGeometricCtrl;

/* Definitions for geometricCtrlPosTask */
extern TaskHandle_t geometricCtrlPosTaskHandle;
extern void geometricCtrlPos_main(void *argument);
extern SemaphoreHandle_t semGeometricCtrlPos;

/* Definitions for positionCtrlTask */
extern TaskHandle_t positionCtrlTaskHandle;
extern void positionCtrl_main(void *argument);
extern SemaphoreHandle_t semPositionCtrl;

/* Definitions for tranReceiveTask */
extern TaskHandle_t tranReceiveTaskHandle;
extern void tranReceive_main(void *argument);

/* Definitions for flowTask */
extern TaskHandle_t flowTaskHandle;
extern void flow_main(void *argument);

/* Definitions for ms5611Task */
extern TaskHandle_t ms5611TaskHandle;
extern void ms5611_main(void *argument);
extern SemaphoreHandle_t semMs5611;

/* Definitions for mahonyAhrsTask */
extern TaskHandle_t mahonyAhrsTaskHandle;
extern void mahonyAhrs_main(void *argument);
extern SemaphoreHandle_t semMahonyAhrs;

/* Definitions for uwbTask */
extern TaskHandle_t uwbTaskHandle;
extern void uwb_main(void *argument);

/* Definitions for ekfTask */
extern TaskHandle_t ekfTaskHandle;
extern void ekf_main(void *argument);
extern SemaphoreHandle_t semEkf;


/* Definitions for trajectoryTask */
extern TaskHandle_t trajectoryTaskHandle;
extern void trajectory_main(void *argument);


/* Definitions for raspberrypiTask */
extern TaskHandle_t raspberrypiTaskHandle;
extern void raspberrypi_main(void *argument);
extern SemaphoreHandle_t semRaspberrypi;

/* Definitions for raspberrypiReceiveTask */
extern TaskHandle_t raspberrypiReceiveTaskHandle;
extern void raspberrypiReceive_main(void *argument);

/* Definitions for safecheckTask */
extern TaskHandle_t safecheckTaskHandle;
extern void safecheck_main(void *argument);
extern SemaphoreHandle_t semSafeCheck;
//自己创建的任务


extern QueueHandle_t queueGyrDat;
extern QueueHandle_t queueGyrDatFil;
extern QueueHandle_t queueAccDat;
extern QueueHandle_t queueMagDat;
extern QueueHandle_t queueBaroAlt;
extern QueueHandle_t queueAhrsEuler;
extern QueueHandle_t queueBattery;
extern QueueHandle_t queueRCData;
extern QueueHandle_t queueRCCommand;
extern QueueHandle_t queueMotorPWM;
extern QueueHandle_t queuePID;
extern QueueHandle_t queueInloopControl;
extern QueueHandle_t queueFlow;
extern QueueHandle_t queueHeight;
extern QueueHandle_t queueMahonyAhrs;				//MahonyAhrs解算得到的位姿
extern QueueHandle_t queueUWB;
extern QueueHandle_t queueCb2n;
extern QueueHandle_t queueUwbPosVel;		//Uwb加速度融合得到的位置速度
extern QueueHandle_t queueKalUwbPosVel;		//Uwb卡尔曼滤波得到的位置速度
extern QueueHandle_t queueMotorRps;		//电机转速
extern QueueHandle_t queueQuaternion;		//四元数
extern QueueHandle_t queueDeltaImu;		//增量角度和增量速度
extern QueueHandle_t queueDeltaImuToZeroFlag;		//增量角度和增量速度置零
extern QueueHandle_t queueEKF;		//EKF输出
extern QueueHandle_t queueAttitude;
extern QueueHandle_t queueControlMsg;

///////////////////////////adding//////////////////////////////////////////////
extern QueueHandle_t queueAddingR;
extern QueueHandle_t queueMoments;
///////////////////////////////////////////////////////////////////////////////

extern QueueHandle_t queueMOCAP;

extern TimerHandle_t myTimer1ms;

void Timer1msCallback(void *argument);

void TIM5_IRQHandler(void);


void mySystemInit(void);

#ifdef __cplusplus
}
#endif

#ifdef __cplusplus
using namespace std;
class SYSTEM
{
public:
	SYSTEM();
	~SYSTEM();
	void taskCreat();		//任务创建
	void semCreat();		//信号量创建
	void timerCreat();		//软件定时器创建
	void queueCreat();		//消息队列创建

	uint32_t  startTimer;			//计时器
	uint32_t  stopTimer;			//计时器
	uint32_t  executionTime_us;			//计时器
	uint32_t  startTimerLast;
	uint32_t  cycleTime_us;

private:
	BaseType_t isIcm20602TaskCreatSuccess;		//陀螺仪数据采集任务创建成功标志位
	BaseType_t isMagTaskCreatSuccess;		//磁力计数据采集任务创建成功标志位
	BaseType_t isAhrsTaskCreatSuccess;		//姿态解算任务创建成功标志位
	BaseType_t isMahonyAhrsTaskCreatSuccess;		//姿态解算任务创建成功标志位
	BaseType_t isEkfUwb1msTaskCreatSuccess;		//位置速度解算任务创建成功标志位
	BaseType_t isEkfUwb5msTaskCreatSuccess;		//位置速度解算任务创建成功标志位
	BaseType_t isKalUwbTaskCreatSuccess;
	BaseType_t isKalUwbPredTaskCreatSuccess;

	BaseType_t isTranTaskCreatSuccess;		//数据传输任务创建成功标志位
	BaseType_t isTranReceiveTaskCreatSuccess;		//数据传输任务创建成功标志位
	BaseType_t isBatteryTaskCreatSuccess;		//电池电压测量任务创建成功标志位
	BaseType_t isRcTaskCreatSuccess;		//遥控器解析任务创建成功标志位
	BaseType_t isMotorTaskCreatSuccess;		//电机驱动任务创建成功标志位
	BaseType_t isAttitudeCtrlTaskCreatSuccess;  //姿态控制任务创建成功标志位
	BaseType_t isGeometricCtrlTaskCreatSuccess;  //姿态控制任务创建成功标志位
	BaseType_t isGeometricCtrlPosTaskCreatSuccess;
	BaseType_t isPositionCtrlTaskCreatSuccess;  //位置控制任务创建成功标志位
	BaseType_t isFlowTaskCreatSuccess;  //姿态控制任务创建成功标志位
	BaseType_t isMs5611TaskCreatSuccess;  //气压高度采集任务创建成功标志位
	BaseType_t isUwbTaskCreatSuccess;  //uwb任务创建成功标志位
	BaseType_t isEkfTaskCreatSuccess;		//姿态解算任务创建成功标志位
	BaseType_t isTrajectoryTaskCreatSuccess;		//轨迹规划姿态解算任务创建成功标志位
	BaseType_t isRaspberrypiTaskCreatSuccess;		//发送数据到树莓派任务创建成功标志位
	BaseType_t isRaspberrypiReceiveTaskCreatSuccess;		//接收树莓派数据任务创建成功标志位
	BaseType_t isSafeCheckTaskCreateSuccess;                 //安全检查任务创建成功标志位


};

#endif

#endif

/************************ (C) COPYRIGHT Longmen Drone Team *****END OF FILE****/
